package com.mentaleye.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.LoanRecordBean;
import com.mentaleye.biz.LoanRecordBiz;
import com.mentaleye.biz.LoanTypeBiz;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Controller
@RequestMapping("/loanRecordController")
public class LoanRecordController {

	@Autowired private LoanTypeBiz typeBiz;
	
	@Autowired private LoanRecordBiz recordBiz;
	
	@RequestMapping("/toPage")
	public String toPage(){
		return "/lm/loanRecord/loanRecordList";
	}
	
	@RequestMapping("/toInsertPage")
	public ModelAndView toInsertPage(){
		ModelAndView view=new ModelAndView("/lm/loanRecord/loanRecordAdd");
		view.addObject("typeBeanList", this.typeBiz.getBeanList());
		return view;
	}
	
	@RequestMapping("/toEditPage")
	public ModelAndView toEditPage(@RequestParam long recordId){
		ModelAndView view=new ModelAndView("/lm/loanRecord/loanRecordEdit");
		view.addObject("typeBeanList", this.typeBiz.getBeanList());
		view.addObject("recordBean", this.recordBiz.getBeanById(recordId));
		return view;
	}
	
	@RequestMapping("/insertRecord")
	@ResponseBody
	public String insertRecord(LoanRecordBean recordBean){
		CommonResult result = this.recordBiz.insertRecord(recordBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public String updateRecord(LoanRecordBean recordBean){
		CommonResult result = this.recordBiz.updateRecord(recordBean);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public String updateRecord(@RequestParam long recordId){
		CommonResult result = this.recordBiz.deleteByKey(recordId);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping("/getList")
	@ResponseBody
	public String getList(CommonPage page) {
		this.recordBiz.getListByPage(page);
		return page.toSHowPage();
	}
}
